/*
 * @Author: xujianxing
 * @Date: 2020-02-15 19:20:07
 * @LastEditors  : xujianxing
 * @LastEditTime : 2020-02-22 17:45:06
 * @Description: file content
 */
/**
 * 初始化数据库,创建表,插入初始数据
 */

const mysql = require("mysql");
var config = require("./config");
function init() {
  var connection = mysql.createConnection(config);
  const initSql = `
CREATE TABLE IF NOT EXISTS zhipin.user (
  zid varchar(36) NOT NULL,
  username varchar(255) NOT NULL unique,
  password varchar(255) NOT NULL,
  usertype varchar(5) NOT NULL,
  avatar varchar(255) DEFAULT NULL,
  position varchar(255) DEFAULT NULL,
  company varchar(255) DEFAULT NULL,
  salary varchar(255) DEFAULT NULL,
  info varchar(255) DEFAULT NULL,
  create_time varchar(20) DEFAULT NULL,
  PRIMARY KEY (zid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO zhipin.user (zid,username,password,usertype,create_time) VALUES(REPLACE(UUID(),'-',''),'admin','123123','0','1574565356');

CREATE TABLE IF NOT EXISTS zhipin.message (
  zid varchar(36) NOT NULL,
  from_id varchar(36) NOT NULL,
  to_id varchar(36) NOT NULL,
  chart_id varchar(255) NOT NULL,
  content varchar(255) DEFAULT NULL,
  isread tinyint(2) DEFAULT 0,
  create_time varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`;
  connection.query(initSql, (err, vals, fields) => {
    if (!err) {
      console.log("数据库初始化完成");
    } else {
      console.log("数据库初始化失败" + err.sqlMessage);
    }
    connection.end(err => {
      if (err) {
        console.log("关闭异常" + err.sqlMessage);
        return;
      }
      console.log("初始化连接关闭");
    });
  });
}
init();
